package com.mystery.action;

import com.mystery.pojo.Order;
import com.mystery.pojo.User;
import com.mystery.service.FitnessService;
import com.mystery.service.OrderService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


public class OrderAction extends ActionSupport implements ModelDriven<Order> {

    private Order order = new Order();

    private List<Order> orderList;

    public List<Order> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<Order> orderList) {
        this.orderList = orderList;
    }

    @Autowired
    private OrderService orderService;

    @Autowired
    private FitnessService fitnessService;


    public String query(){
        HttpServletRequest request = ServletActionContext.getRequest();
        User user = (User) request.getSession().getAttribute("user");
        long userId = user.getId();
        orderList = orderService.queryAll(userId);
        return "query";
    }

    public String paid(){
        HttpServletRequest request = ServletActionContext.getRequest();
        Long id = (Long) request.getAttribute("id");
        orderService.paid(id);
        return "paid";
    }

    public String myEvaluate(){
        HttpServletRequest request = ServletActionContext.getRequest();
        String fitnessName = request.getParameter("fitnessName");
        User user = (User) request.getSession().getAttribute("user");
        long userId = user.getId();
        if(!"".equals(fitnessName)&& fitnessName != null){
           fitnessName = "%"+fitnessName+"%";
        }
        orderList = orderService.query(userId,fitnessName);
        return "evaluate";
    }

    public String goEvaluate(){
        HttpServletRequest request = ServletActionContext.getRequest();
        Long id = (Long) request.getAttribute("id");
        request.setAttribute("orderId",id);
        return "goEvaluate";
    }

    public String update(){
        HttpServletRequest request = ServletActionContext.getRequest();
        String id =  request.getParameter("orderId");
        String evaluate = request.getParameter("evaluate");
        orderService.update(Long.parseLong(id),evaluate);
        return "update_ok";
    }

    public String add(){
        HttpServletRequest request = ServletActionContext.getRequest();
        String Id = request.getParameter("fitnessId");
        long fitnessId = Long.parseLong(Id);
        User user = (User) request.getSession().getAttribute("user");
        String fitnessName = fitnessService.findFitnessNameById(fitnessId);
        long orderNumber = fitnessId+100L;
        orderService.add(user.getId(),user.getUsername(),fitnessId,fitnessName,orderNumber);
        return "add_ok";
    }

    public String find(){
        HttpServletRequest request = ServletActionContext.getRequest();
        User user = (User) request.getSession().getAttribute("user");
        long userId = user.getId();
        orderList =orderService.find(userId);
        return "find";
    }
    @Override
    public Order getModel() {
        return order;
    }
}
